package com.sixmac.service.impl;

import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.dao.MalfunctionDao;
import com.sixmac.entity.Malfunction;
import com.sixmac.service.MalfunctionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19 0019 上午 10:12.
 */
@Service
public class MalfunctionServiceImpl implements MalfunctionService {

    @Autowired
    private MalfunctionDao malfunctionDao;

    @Override
    public List<Malfunction> findAll() {
        return malfunctionDao.findAll();
    }

    @Override
    public Page<Malfunction> find(int pageNum, int pageSize) {
        return malfunctionDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Malfunction> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Malfunction getById(int id) {
        return malfunctionDao.findOne(id);
    }

    @Override
    public Malfunction deleteById(int id) {
        Malfunction malfunction = getById(id);
        malfunctionDao.delete(malfunction);
        return malfunction;
    }

    @Override
    public Malfunction create(Malfunction malfunction) {
        return malfunctionDao.save(malfunction);
    }

    @Override
    public Malfunction update(Malfunction malfunction) {
        return malfunctionDao.save(malfunction);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Malfunction> findListNew() {
        return malfunctionDao.findListNew(CommonController.getOldDate());
    }

    @Override
    public List<Malfunction> findListByStatus(Integer status) {
        return malfunctionDao.findListByStatus(status);
    }

    @Override
    public Page<Malfunction> pageByParams(final Integer id, final String mobile, final Integer type, final Integer areaId, final Integer status, Integer pageNum, Integer pageSize) {
        Page<Malfunction> page = malfunctionDao.findAll(new Specification<Malfunction>() {
            @Override
            public Predicate toPredicate(Root<Malfunction> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (null != id) {
                    Predicate pre = cb.equal(root.get("id").as(Integer.class), id);
                    predicateList.add(pre);
                }
                if (StringUtils.isNotBlank(mobile)) {
                    Predicate pre = cb.like(root.get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }
                if (null != type) {
                    Predicate pre = cb.equal(root.get("type").as(Integer.class), type);
                    predicateList.add(pre);
                }
                if (null != areaId) {
                    Predicate pre = cb.equal(root.get("area").get("id").as(Integer.class), areaId);
                    predicateList.add(pre);
                }
                if (null != status) {
                    Predicate pre = cb.equal(root.get("status").as(Integer.class), status);
                    predicateList.add(pre);
                }
                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));

        return page;
    }
}